package com.heima.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.constants.UserConstants;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.AuthDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.ApUserRealnameService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@RequiredArgsConstructor
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {


    private final ApUserRealnameMapper apUserRealnameMapper;

    private final ApUserMapper apUserMapper;


    /**
     * 用户未认证列表
     *
     * @param authDto
     * @return
     */
    @Override
    public ResponseResult listApUserUnAuthenticated(AuthDto authDto) {
        // 1、检查参数
        if (authDto == null || authDto.getPage() < 0 || authDto.getSize() <= 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 2、分页参数检查
        authDto.setPage((authDto.getPage() - 1) * authDto.getSize());
        List<ApUserRealname> list = apUserRealnameMapper.listApUserUnAuthenticated(authDto);
        return ResponseResult.okResult(list);
    }

    /**
     * 审核失败 或者通过
     *
     * @param authDto
     * @return
     */
    @Override
    public ResponseResult ApUserRealnameAuthFail(AuthDto authDto) {
        if (authDto == null || authDto.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 更新ap_user_realname表
        ApUserRealname apUserRealname=new ApUserRealname();
        apUserRealname.setId(authDto.getId());
        apUserRealname.setStatus(UserConstants.USER_AUTH_FAIL);
        if(authDto.getMsg()!=null){
            apUserRealname.setReason(authDto.getMsg());
        }
        apUserRealnameMapper.ApUserRealnameAuthFailOrPass(apUserRealname);

        //同时更新 ao_user表
        ApUser apUser=new ApUser();
        apUser.setId(authDto.getId());
        apUser.setIdentityAuthentication(false);
        apUserMapper.updateByEntity(apUser);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 审核通过
     *
     * @param authDto
     * @return
     */
    @Override
    public ResponseResult ApUserRealnameAuthPass(AuthDto authDto) {
        if (authDto == null || authDto.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUserRealname apUserRealname=new ApUserRealname();
        apUserRealname.setId(authDto.getId());
        apUserRealname.setStatus(UserConstants.USER_AUTH_PASS);
        if(authDto.getMsg()!=null){
            apUserRealname.setReason(authDto.getMsg());
        }
        apUserRealnameMapper.ApUserRealnameAuthFailOrPass(apUserRealname);
        //同时更新 ao_user表
        ApUser apUser=new ApUser();
        apUser.setId(authDto.getId());
        apUser.setIdentityAuthentication(true);
        apUserMapper.updateByEntity(apUser);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
